package trabajoISW2.data;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import trabajoISW2.domain.implementations.User;

public class UserDAO extends BaseDAO implements IUserDAO {

	public UserDAO(){
		
	}
	@Override
	public List<User> getUsers() {

		Query q = getEm().createQuery("Select u FROM User u");
		return q.getResultList();

	}

	@Override
	public User getUser(String id) {
		return getEm().find(User.class, id);
	}

	@Override
	public User createUser(User u) {
		getEm().getTransaction().begin();
		getEm().persist(u);
		getEm().getTransaction().commit();
		getEm().close();
		return u;
	}

	@Override
	public void updateUser(User u) {

		getEm().merge(u);

	}

	@Override
	public void deleteUser(String id) {

		User user = getEm().find(User.class, id);
		if (user != null){
			getEm().getTransaction().begin();
			getEm().remove(user);
			getEm().getTransaction().commit();
			getEm().close();
		}

	}

}
